package com.runccyy.web;


import com.runccyy.dao.model.RunRecordPO;
import com.runccyy.dao.model.RunRecordStatPO;
import com.runccyy.dao.model.StepInfo;
import com.runccyy.exception.BizException;
import com.runccyy.service.RunRecordService;
import com.runccyy.service.model.RunRecordStatBO;
import com.runccyy.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/run")
@Api(value = "RunRecordController", description = "步数记录相关操作")
public class RunRecordController {

    @Resource
    private RunRecordService runRecordService;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/saveRecords")
    @ApiOperation(value = "saveRecords", notes = "录入用户步数记录")
    public void insertOrUpdateRunRecord(@ApiParam(value = "步数记录", required = true) @RequestBody List<RunRecordPO> runRecords,
                                        @ApiParam(value = "用户姓名", required = true)@RequestHeader String auth) {
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        if (CollectionUtils.isEmpty(runRecords)){
            return;
        }
        runRecordService.insertOrUpdateRunRecord(runRecords);
    }

    @GetMapping("queryRecord")
    @ApiOperation(value = "queryRecord", notes = "根据用户id和时间查询步数记录")
    public RunRecordPO getRunRecordByUserIdAndDate(@ApiParam(value = "用户id", required = true) @RequestParam Long userId,
                                                   @ApiParam(value = "日期", required = true) @RequestParam Long date,
                                                   @ApiParam(value = "用户姓名", required = true)@RequestHeader String auth) {
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        return runRecordService.getRunRecordByUserIdAndDate(userId, date);
    }

    @GetMapping("/dayRank")
    @ApiOperation(value = "dayRank", notes = "用户记录天数排名")
    public List<RunRecordStatBO> countDaysWithStepCntRnk(@ApiParam(value = "开始时间", required = true)@RequestParam Long startTime,
                                                         @ApiParam(value = "结束时间", required = true)@RequestParam Long endTime,
                                                         @ApiParam(value = "用户姓名", required = true)@RequestHeader String auth){
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        if (startTime == null || endTime== null){
            throw new BizException("601","开始和结束时间不能为空");
        }
        return runRecordService.countDaysRank(startTime, endTime);
    }

    @GetMapping("/stepCntRank")
    @ApiOperation(value = "stepCntRank", notes = "步数排行榜")
    public List<RunRecordStatBO> sumStepCntByUserId(@ApiParam(value = "开始时间", required = true)@RequestParam Long startTime,
                                                    @ApiParam(value = "结束时间", required = true)@RequestParam Long endTime,
                                                    @ApiParam(value = "用户姓名", required = true)@RequestHeader String auth){
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        if (startTime == null || endTime== null){
            throw new BizException("601","开始和结束时间不能为空");
        }
        return runRecordService.sumStepCntRank(startTime, endTime);
    }

    @GetMapping("/stepInfo")
    @ApiOperation(value = "stepInfo", notes = "步数排行榜")
    public StepInfo stepInfo(@ApiParam(value = "userId", required = true)@RequestParam Long userId,
                             @ApiParam(value = "用户姓名", required = true)@RequestHeader String auth){
        if (!redisUtil.hasKey(auth)){
            throw new BizException("600","鉴权错误，请重新登录");
        }
        if (userId == null){
            throw new BizException("601","userId不能为空");
        }
        return runRecordService.queryStepInfo(userId);
    }
}
